package com.ambitor.grass.sql.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ambitor.grass.sql.dao.BaseDao;
import com.ambitor.grass.sql.data.Parameter;
import com.ambitor.grass.sql.statement.BatchOperation;
import com.ambitor.grass.sql.statement.BatchParameterStatementImpl;
import com.ambitor.grass.sql.statement.BatchPreStatementImpl;
import com.ambitor.grass.sql.statement.BatchStatementImpl;
import com.ambitor.grass.sql.statement.DefaultOperation;
import com.ambitor.grass.sql.statement.DefaultParameterStatementImpl;
import com.ambitor.grass.sql.statement.DefaultPreStatementImpl;
import com.ambitor.grass.sql.statement.DefaultStatementImpl;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;

public class BaseDaoImpl implements BaseDao {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	private Connection conn;

	public BaseDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public Connection getConnection() {
		return conn;
	}

	public ResultSet executeQuery(String sql) throws Exception {
		DefaultOperation operation = new DefaultStatementImpl(conn, sql);
		logger.info("无参数SQL: " + sql + " 将被执行");
		ResultSet rs = operation.executeQuery();
		logger.info("无参数SQL: " + sql + " 执行完毕");
		return rs;
	}

	public ResultSet executeQuery(String sql, Parameter param) throws Exception {
		DefaultOperation operation = new DefaultParameterStatementImpl(conn,
				sql, param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		for (int i = 0; i < param.size(); i++) {
			Object obj = param.get(i);
			if (obj == null)
				obj = "null";
			logger.info("参数" + (i + 1) + ":" + obj.toString());
		}
		ResultSet rs = operation.executeQuery();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		return rs;
	}

	public ResultSet executeQuery(String sql, IData param) throws Exception {
		DefaultOperation operation = new DefaultPreStatementImpl(conn, sql,
				param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		String[] names = param.getNames();
		for (int i = 0; i < names.length; i++) {
			Object obj = param.get(names[i]);
			if (obj == null)
				obj = "null";
			logger.info(names[i] + ":" + obj.toString());
		}
		ResultSet rs = operation.executeQuery();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		return rs;
	}

	public int executeUpdate(String sql) throws Exception {
		DefaultOperation operation = new DefaultStatementImpl(conn, sql);
		logger.info("无参数SQL: " + sql + " 将被执行");
		int result = operation.executeUpdate();
		logger.info("无参数SQL: " + sql + " 执行完毕");
		logger.info(result + "条记录被更新或删除.");
		return result;
	}

	public String executeInsertAndReturnLastInsertId(String sql)
			throws Exception {
		DefaultOperation operation = new DefaultStatementImpl(conn, sql);
		logger.info("无参数SQL: " + sql + " 将被执行");
		String result = operation.executeInsertAndReturnLastInsertId();
		logger.info("无参数SQL: " + sql + " 执行完毕");
		logger.info("1条记录被更新或删除，返回主键为."+result);
		return result;
	}

	public int executeUpdate(String sql, Parameter param) throws Exception {
		DefaultOperation operation = new DefaultParameterStatementImpl(conn,
				sql, param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		for (int i = 0; i < param.size(); i++) {
			Object obj = param.get(i);
			if (obj == null)
				obj = "null";
			logger.info("参数" + (i + 1) + ":" + obj.toString());
		}
		int result = operation.executeUpdate();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		logger.info(result + "条记录被更新或删除.");
		return result;
	}

	public String executeInsertAndReturnLastInsertId(String sql, Parameter param)
			throws Exception {
		DefaultOperation operation = new DefaultParameterStatementImpl(conn,
				sql, param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		for (int i = 0; i < param.size(); i++) {
			Object obj = param.get(i);
			if (obj == null)
				obj = "null";
			logger.info("参数" + (i + 1) + ":" + obj.toString());
		}
		String result = operation.executeInsertAndReturnLastInsertId();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		logger.info("1条记录被更新或删除，返回主键为."+result);
		return result;
	}

	public int executeUpdate(String sql, IData param) throws Exception {
		DefaultOperation operation = new DefaultPreStatementImpl(conn, sql,
				param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		String[] names = param.getNames();
		for (int i = 0; i < names.length; i++) {
			Object obj = param.get(names[i]);
			if (obj == null)
				obj = "null";
			logger.info(names[i] + ":" + obj.toString());
		}
		int result = operation.executeUpdate();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		logger.info(result + "条记录被更新或删除.");
		return result;
	}

	public String executeInsertAndReturnLastInsertId(String sql, IData param)
			throws Exception {
		DefaultOperation operation = new DefaultPreStatementImpl(conn, sql,
				param);
		logger.info("带参数SQL: " + sql + " 将被执行");
		String[] names = param.getNames();
		for (int i = 0; i < names.length; i++) {
			Object obj = param.get(names[i]);
			if (obj == null)
				obj = "null";
			logger.info(names[i] + ":" + obj.toString());
		}
		String result = operation.executeInsertAndReturnLastInsertId();
		logger.info("带参数SQL: " + sql + " 执行完毕");
		logger.info("1条记录被更新或删除，返回主键为."+result);
		return result;
	}

	public int[] executeBatch(String[] sqls) throws Exception {
		BatchOperation operation = new BatchStatementImpl(conn, sqls);
		for (String sql : sqls)
			logger.info("无参数SQL: " + sql + " 将被批量执行");
		int[] results = operation.executeBatch();
		for (String sql : sqls)
			logger.info("无参数SQL: " + sql + " 被批量执行完毕");
		return results;
	}

	public int[] executeBatch(String sql, Parameter[] params) throws Exception {
		BatchOperation operation = new BatchParameterStatementImpl(conn, sql,
				params);
		logger.info("带参数SQL: " + sql + " 将被批量执行");
		for (Parameter param : params) {
			for (int i = 0; i < param.size(); i++) {
				Object obj = param.get(i);
				if (obj == null)
					obj = "null";
				logger.info("参数" + (i + 1) + ":" + obj.toString());
			}
		}
		int[] results = operation.executeBatch();
		logger.info("带参数SQL: " + sql + " 被批量执行完毕");
		return results;
	}

	public int[] executeBatch(String sql, IDataset params) throws Exception {
		BatchOperation operation = new BatchPreStatementImpl(conn, sql, params);
		logger.info("带参数SQL: " + sql + " 将被批量执行");
		for (int i = 0; i < params.count(); i++) {
			IData param = params.getData(i);
			String[] names = param.getNames();
			for (int j = 0; j < names.length; j++) {
				Object obj = param.get(names[j]);
				if (obj == null)
					obj = "null";
				logger.info(names[j] + ":" + obj.toString());
			}
		}
		int[] results = operation.executeBatch();
		logger.info("带参数SQL: " + sql + " 被批量执行完毕");
		return results;
	}
}
